package mysql

import (
	"errors"
	"fmt"
	"gitee.com/captials-team/ubdframe/src/domain/configstc"
	"gitee.com/captials-team/ubdframe/src/domain/dto"
	"gitee.com/captials-team/ubdframe/src/domain/dto/paginate"
	"gitee.com/captials-team/ubdframe/src/domain/interfaces"
	"gitee.com/captials-team/ubdframe/src/domain/models"
	mysqlClients "gitee.com/captials-team/ubdframe/src/infrastructure/clients/mysql"
	"gorm.io/gorm"
)

// NewRbacRoleDao new
func NewRbacRoleDao(conf configstc.DBConfig) *RbacRoleDao {
	return &RbacRoleDao{
		db:   mysqlClients.NewGormDB(conf),
		conf: conf,
	}
}

type RbacRoleDao struct {
	db   *gorm.DB
	conf configstc.DBConfig
}

func (dao *RbacRoleDao) Use(db *gorm.DB) interfaces.ItfRbacRole {
	return &RbacRoleDao{db: db}
}

func (dao *RbacRoleDao) DB() *gorm.DB {
	return dao.db
}

// Search 查找角色
func (dao *RbacRoleDao) Search(search *dto.SearchRbacRoleParams, pa *paginate.Pager) ([]*models.RbacRole, *paginate.Pager, error) {
	if search == nil {
		search = &dto.SearchRbacRoleParams{}
	}
	pa.Correct()
	var list []*models.RbacRole

	db := dao.db.Model(new(models.RbacRole))

	if search.Domain != "" {
		db = db.Where("domain=?", search.Domain)
	}
	if search.Status > 0 {
		db = db.Where("status=?", search.Status)
	}
	if search.IsSimple {
		db = db.Select("id", "name", "sort", "domain", "status")
	}
	if ret := db.Count(&pa.Total); ret.Error != nil {
		return list, pa, ret.Error
	}

	if !search.IsAll {
		db = db.Limit(pa.Size).Offset(pa.Offset())
	}
	db = db.Order("status asc").
		Order("sort desc").
		Order("id asc")

	if ret := db.Find(&list); ret.Error != nil {
		return list, pa, ret.Error
	}
	pa.Correct()
	if search.IsAll {
		pa.Size = int(pa.Total)
		pa.PageCount = 1
		pa.Page = 1
	}
	return list, pa, nil
}

func (dao *RbacRoleDao) Query(id int64) (*models.RbacRole, error) {
	var info models.RbacRole
	ret := dao.db.Model(&info).First(&info, "id = ?", id)
	if errors.Is(ret.Error, gorm.ErrRecordNotFound) {
		return nil, nil
	}
	if ret.Error != nil {
		return nil, ret.Error
	}
	return &info, nil
}

func (dao *RbacRoleDao) Add(add *models.RbacRole) (int64, error) {
	if add.Id > 0 {
		return 0, fmt.Errorf("not support update")
	}
	ret := dao.db.Model(add).Create(add)
	if ret.Error != nil {
		return 0, ret.Error
	}
	if ret.RowsAffected == 0 {
		return 0, fmt.Errorf("add fail")
	}

	return add.Id, nil
}

func (dao *RbacRoleDao) Update(id int64, up *models.RbacRole) (int64, error) {
	if id <= 0 {
		return 0, fmt.Errorf("not find info")
	}
	up.Id = id
	ret := dao.db.Save(up)
	if ret.Error != nil {
		return 0, ret.Error
	}
	if ret.RowsAffected == 0 {
		return 0, fmt.Errorf("no update")
	}
	return ret.RowsAffected, nil
}

func (dao *RbacRoleDao) Delete(id int64) error {
	ret := dao.db.Where("id=?", id).Delete(&models.RbacRole{})
	if ret.Error != nil {
		return ret.Error
	}
	if ret.RowsAffected == 0 {
		return fmt.Errorf("no update")
	}
	return nil
}

func (dao *RbacRoleDao) ResetStatus(id int64, status int) (int64, error) {
	// 根据条件更新
	db := dao.db.Model(&models.RbacRole{})
	ret := db.Where("id = ?", id).Update("status", status)
	return ret.RowsAffected, ret.Error
}

func (dao *RbacRoleDao) Migrate() (int64, error) {
	return 4, dao.db.AutoMigrate(
		new(models.RbacRole),
	)
}
